﻿#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

namespace Game.Editor
{
    /// <summary>
    /// 弹出式window
    /// </summary>
	public class Editor_ObjSettingWindow : EditorWindow
    {
        Editor_ObjData manager;
        public void Init(Editor_ObjData _manager)
        {
            manager = _manager;
            //编辑器的弹出窗口最小大小
            minSize = new Vector2(200, 256);    
        }

        Vector2 scrollViewV2 = Vector2.zero;
        void OnGUI()
        {
            if (!manager)
                return;
            scrollViewV2 = GUILayout.BeginScrollView(scrollViewV2, GUILayout.ExpandWidth(false));

            DrawMapSetting();

            GUILayout.Space(16);

            DrawViewSetting();

            GUILayout.EndScrollView();
        }

        bool showMapSetting = true;
        string[] select3D_2DText = { "Plane Map", "3D Map" };
        private void DrawMapSetting()
        {
            showMapSetting = EditorGUILayout.Foldout(showMapSetting, "Map setting ");
            if (!showMapSetting)
                return;

            EditorGUILayout.HelpBox("Select a type of your map:\n x-y plane Map or x-y-z 3D Map?", MessageType.Info);

            GUILayout.BeginHorizontal();
            GUILayout.Label("Type:");

            //bool is3DMap = manager.is3DMap;
            bool is3DMap = true;
            //转换成选择视图：
            int selectIndex = is3DMap ? 1 : 0;
            selectIndex = GUILayout.SelectionGrid(selectIndex, select3D_2DText, 2);
            is3DMap = (selectIndex == 1);

            //if (manager.is3DMap != is3DMap)
            //{
            //    manager.is3DMap = is3DMap;
            //    //应用更改：
            //    SceneView.RepaintAll();
            //}

            GUILayout.EndHorizontal();

            GUILayout.Label("Set up your map:");
            //manager.isCubeTileBlock = EditorGUILayout.Toggle("  Is cube block:", manager.isCubeTileBlock);
            //int inputValue;
            //float scaleFactor;
            //if (manager.isCubeTileBlock)
            //{
            //    inputValue = manager.blockWidth;

            //    if (DrawIntFieldChanged("  Block Size:", ref inputValue))
            //    {
            //        scaleFactor = ((float)inputValue) / manager.blockWidth;
            //        //Changed:
            //        manager.blockWidth = inputValue;
            //        manager.blockHeight = inputValue;
            //        manager.blockThickness = inputValue;
            //        manager.ResizeBlocks(scaleFactor, scaleFactor, scaleFactor);
            //    }

            //}
            //else
            //{
            //    inputValue = manager.blockWidth;
            //    if (DrawIntFieldChanged("    block width:", ref inputValue))
            //    {
            //        scaleFactor = ((float)inputValue) / manager.blockWidth;
            //        manager.blockWidth = inputValue;
            //        manager.ResizeBlocks(scaleFactor);
            //    }

            //    inputValue = manager.blockHeight;
            //    if (DrawIntFieldChanged("    block height:", ref inputValue))
            //    {
            //        scaleFactor = ((float)inputValue) / manager.blockHeight;
            //        manager.blockHeight = inputValue;
            //        manager.ResizeBlocks(1, scaleFactor);
            //    }

            //    inputValue = manager.blockThickness;
            //    if (DrawIntFieldChanged("    block thickness:", ref inputValue))
            //    {
            //        scaleFactor = ((float)inputValue) / manager.blockThickness;
            //        manager.blockThickness = inputValue;
            //        manager.ResizeBlocks(1, 1, scaleFactor);
            //    }
                //manager.blockWidth = EditorGUILayout.IntField( "    block width:" , manager.blockWidth );
                //manager.blockHeight = EditorGUILayout.IntField( "    block height:" , manager.blockHeight );
                //manager.blockThickness = EditorGUILayout.IntField( "    block scaleZ:" , manager.blockThickness );
            //}


            //EditorGUILayout.HelpBox("If you have drawed a map, changing the size will make some bricks in a wrong place.", MessageType.Warning);


        }

        bool showSettingView = false;
        private void DrawViewSetting()
        {
            showSettingView = EditorGUILayout.Foldout(showSettingView, "View Setting");

            if (showSettingView)
            {
                //Draw option:网格显示
                _DrawGridOptionView();
                GUILayout.Space(4);

                //操作提示：
                _DrawHintView();

                GUILayout.Space(16);
            }
        }

        //是否在场景中显示网格（xz平面）
        private void _DrawGridOptionView()
        {
            //bool last = manager.showGrid;
            //manager.showGrid = GUILayout.Toggle(manager.showGrid, "Show grid");
            //if (manager.showGrid != last)               //避免重绘多次
            //{
            //    SceneView.RepaintAll();
            //}

            //manager.gridColor = EditorGUILayout.ColorField("Grid color:", manager.gridColor);

        }

        private bool DrawIntFieldChanged(string strText, ref int valSrc)
        {
            int tmpInputVal = valSrc;
            tmpInputVal = EditorGUILayout.IntField(strText, tmpInputVal);
            if (tmpInputVal <= 0)
                tmpInputVal = 1;
            else if (tmpInputVal >= 99)
                tmpInputVal = 99;

            if (tmpInputVal != valSrc)
            {
                //Apply value change
                valSrc = tmpInputVal;
                return true;
            }
            else
            {
                return false;
            }
        }

        bool showHint = false;
        private void _DrawHintView()
        {
            showHint = EditorGUILayout.Foldout(showHint, "Hint");

            if (showHint)
            {
                //if (manager.m_toolStatus != BlocksManager.ToolStatus.BATCH_TOOL)
                {
                    EditorGUILayout.HelpBox("Shift + Click to erase objects painted.", MessageType.None);
                    EditorGUILayout.HelpBox("Layer will be ignored when erase in 3D view.", MessageType.None);
                }
                //else
                {
                    EditorGUILayout.HelpBox("You can batch generate a lot of objects here.", MessageType.None);
                    EditorGUILayout.HelpBox("Also, you can clear all blocks.", MessageType.None);
                }
            }

        }
    }
}
#endif